package com.powerdata.system.paimon.impl;

import com.powerdata.core.paimon.PDPaimonUtils;
import com.powerdata.system.domain.param.PaimonCatalogParam;
import com.powerdata.system.paimon.IDataBaseService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author deeprado
 * @version 1.0
 * @description
 * @date 2023/6/12 16:33
 */
@Service
public class DataBaseServiceImpl implements IDataBaseService {
    @Value(value = "${paimonManager.hiveConf}")
    private String uploadHiveFilePath;
    @Value(value = "${paimonManager.hadoopUser}")
    private String hadoopUser;

    @Override
    public List<String> databaseList(PaimonCatalogParam paimonCatalogParam) throws Exception {
        String id = paimonCatalogParam.getId();
        String warehouse = paimonCatalogParam.getHdfsurl();
        String types = paimonCatalogParam.getTypes();
        String hiveUrl = paimonCatalogParam.getHiveurl();

        Map<String, Object> extraProperties = new HashMap<>();
        extraProperties.put("hiveUser", paimonCatalogParam.getHiveuser());
        extraProperties.put("hivePassword", paimonCatalogParam.getHivepassword());
        extraProperties.put("jdbcUri", paimonCatalogParam.getJdbcuri());
        extraProperties.put("jdbcUser", paimonCatalogParam.getJdbcuser());
        extraProperties.put("jdbcPassword", paimonCatalogParam.getJdbcpassword());

        return PDPaimonUtils.build(id, types, hiveUrl, warehouse, hadoopUser, uploadHiveFilePath, extraProperties)
                .listDataBases();
    }

    @Override
    public void addDatabase(PaimonCatalogParam paimonCatalogParam) throws Exception {
        String id = paimonCatalogParam.getId();
        String warehouse = paimonCatalogParam.getHdfsurl();
        String types = paimonCatalogParam.getTypes();
        String hiveUrl = paimonCatalogParam.getHiveurl();

        Map<String, Object> extraProperties = new HashMap<>();
        extraProperties.put("hiveUser", paimonCatalogParam.getHiveuser());
        extraProperties.put("hivePassword", paimonCatalogParam.getHivepassword());
        extraProperties.put("jdbcUri", paimonCatalogParam.getJdbcuri());
        extraProperties.put("jdbcUser", paimonCatalogParam.getJdbcuser());
        extraProperties.put("jdbcPassword", paimonCatalogParam.getJdbcpassword());

        PDPaimonUtils.build(id, types, hiveUrl, warehouse, hadoopUser, uploadHiveFilePath, extraProperties)
                .addDatabases(paimonCatalogParam.getDatabaseName());
    }

    @Override
    public void deleteDatabase(PaimonCatalogParam paimonCatalogParam) throws Exception {
        String id = paimonCatalogParam.getId();
        String warehouse = paimonCatalogParam.getHdfsurl();
        String types = paimonCatalogParam.getTypes();
        String hiveUrl = paimonCatalogParam.getHiveurl();

        Map<String, Object> extraProperties = new HashMap<>();
        extraProperties.put("hiveUser", paimonCatalogParam.getHiveuser());
        extraProperties.put("hivePassword", paimonCatalogParam.getHivepassword());
        extraProperties.put("jdbcUri", paimonCatalogParam.getJdbcuri());
        extraProperties.put("jdbcUser", paimonCatalogParam.getJdbcuser());
        extraProperties.put("jdbcPassword", paimonCatalogParam.getJdbcpassword());
        PDPaimonUtils.build(id, types, hiveUrl, warehouse, hadoopUser, uploadHiveFilePath, extraProperties)
                .deleteDatabases(paimonCatalogParam.getDatabaseName());
    }
}
